import { getQuery } from 'h3';
import { FileModel } from '~/server/models/file.model';

// 获取所有图片文件，支持分页
export default defineEventHandler(async (event): Promise<any> => {
	try {
		const query = getQuery(event);
		const page = Number(query.page) > 0 ? Number(query.page) : 1;
		const pageSize = Number(query.pageSize) > 0 ? Number(query.pageSize) : 10;

		// 只查图片类型
		const filter = { mimeType: { $regex: '^image/', $options: 'i' } };

		const total = await FileModel.countDocuments(filter);
		const list = await FileModel.find(filter)
			.sort({ uploadTime: -1 })
			.skip((page - 1) * pageSize)
			.limit(pageSize)
			.lean();

		return { code: 1, message: '获取成功', data: { list, total } };
	} catch (error: any) {
		return { code: 0, message: error.message };
	}
});
